package com.example.playbox.utils

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteException
import com.example.playbox.bean.Game

class MySQLiteOpenHelper(private val context: Context) {

    private var dbHelper: MyDBHelper? = null
    private var db: SQLiteDatabase? = null

    @Throws(SQLiteException::class)
    fun open() {
        dbHelper = MyDBHelper(context)
        db = try {
            dbHelper!!.writableDatabase
        } catch (ex: SQLiteException) {
            dbHelper!!.readableDatabase
        }
    }

    fun close() {
        db?.close()
        db = null
    }

    fun addGames(game: Game): Long {
        val values = ContentValues().apply {
            put("gameId", game.gameId)
            put("gameName", game.gameName)
            put("gameTime", game.gameTime)
            put("gameNote", game.gameNote)
            put("userName", game.userName)
            put("reserved", if (game.reserved) 1 else 0)
        }
        return db!!.insert("tb_Games", null, values)
    }

    fun deletGames(game: Game): Int {
        return db!!.delete("tb_Games", "gameId=? AND userName=?", arrayOf(game.gameId, game.userName))
    }

    fun updateGames(game: Game): Int {
        val values = ContentValues().apply {
            put("gameName", game.gameName)
            put("gameTime", game.gameTime)
            put("gameNote", game.gameNote)
            put("reserved", if (game.reserved) 1 else 0)
        }
        return db!!.update("tb_Games", values, "gameId=?", arrayOf(game.gameId))
    }

    @SuppressLint("Range")
    fun getGames(gameId: String): Game {
        val cursor = db!!.query("tb_Games", null, "gameId=?", arrayOf(gameId), null, null, null)
        cursor.moveToFirst()
        val game = Game(
            gameId = cursor.getString(cursor.getColumnIndex("gameId")),
            gameName = cursor.getString(cursor.getColumnIndex("gameName")),
            gameTime = cursor.getString(cursor.getColumnIndex("gameTime")),
            gameNote = cursor.getString(cursor.getColumnIndex("gameNote")),
            userName = cursor.getString(cursor.getColumnIndex("userName")),
            reserved = cursor.getInt(cursor.getColumnIndex("reserved")) == 1
        )
        cursor.close()
        return game
    }

    @SuppressLint("Range")
    fun getAllGames(): ArrayList<Map<String, Any>>? {
        val listGames = ArrayList<Map<String, Any>>()
        val cursor = db!!.query("tb_Games", null, null, null, null, null, null)
        val resultCounts = cursor.count
        if (resultCounts == 0) {
            cursor.close()
            return null
        } else {
            while (cursor.moveToNext()) {
                val map = HashMap<String, Any>().apply {
                    put("gameId", cursor.getString(cursor.getColumnIndex("gameId")))
                    put("gameName", cursor.getString(cursor.getColumnIndex("gameName")))
                    put("gameTime", cursor.getString(cursor.getColumnIndex("gameTime")))
                    put("gameNote", cursor.getString(cursor.getColumnIndex("gameNote")))
                    put("reserved", cursor.getInt(cursor.getColumnIndex("reserved")) == 1)
                }
                listGames.add(map)
            }
        }
        cursor.close()
        return listGames
    }
}